[ORG 0x7c00]

BOOT_MAIN_ADDR equ 500h

[SECTION .text]
[BITS 16]
global boot_start
boot_start:
    mov ax, 0
    mov ds, ax
    mov es, ax
    mov ss, ax

    ; 清屏
    mov ax, 3
    int 10h

    mov edi, BOOT_MAIN_ADDR
    mov ecx, 1
    mov bl, 2
    call read_hd

    mov si, jmp_to_setup
    call print
    jmp BOOT_MAIN_ADDR

read_hd:
    mov dx, 0x1f2
    mov al, bl
    out dx, al

    mov dx, 0x1f3
    mov al, cl
    out dx, al

    mov dx, 0x1f4
    mov al, ch
    out dx, al

    shr ecx, 16
    mov dx, 0x1f5
    mov al, cl
    out dx, al

    mov dx, 0x1f6
    and ch, 0b1111
    mov al, 0b1110_0000
    or al, ch
    out dx, al

    mov dx, 0x1f7
    mov al, 0x20
    out dx, al

    xor cx, cx
    mov cl, bl
.start_read:
    push cx

    call wait_hd_prepare
    call read_hd_data
    pop cx
    loop .start_read

    ret

wait_hd_prepare:
    mov dx, 0x1f7
.wait:
    in al, dx
    and al, 0x88
    test al, 0x80
    jnz .wait
    ret

read_hd_data:
    mov dx, 0x1f0
    mov cx, 256
.read_word:
    in ax, dx
    mov [edi], ax
    add edi, 2
    loop .read_word

    ret

print:
    mov ah, 0x0e
    mov bh, 0
    mov bl, 1
.loop:
    mov al, [si]
    test al, al
    jz .end
    int 10h
    inc si
    jmp .loop
.end:
    ret

jmp_to_setup db "jump to setup", 10, 13, 0

times 510 - ($ - $$) db 0
dw 0xaa55